﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace UpwardsTimeClock.Prototype
{
    public class GraphicControl : VisualControlBase<Graphic>
    {
        private readonly PictureBox _pictureBox = new PictureBox();
        private bool _gotFile;

        public GraphicControl(Graphic config): base(config)
        {
        }

        protected override Control CreateVisualControl()
        {
            return _pictureBox;
        }

        public override void Initialize(Control.ControlCollection controls)
        {
            string imagePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images");
            string imageFile = Program.BuildFilePath(imagePath, Config.ImageFile);

            if (imageFile != null)
            {
                _gotFile = true;

                ((ISupportInitialize)_pictureBox).BeginInit();
                _pictureBox.Name = Config.Key;

                base.Initialize(controls);

                switch (Config.DisplayMode)
                {
                    case DisplayMode.Center:
                        _pictureBox.SizeMode = PictureBoxSizeMode.CenterImage;
                        break;
                    case DisplayMode.Scale:
                        _pictureBox.SizeMode = PictureBoxSizeMode.Zoom;
                        break;
                    case DisplayMode.Stretch:
                        _pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
                        break;
                    case DisplayMode.Tile:
                        throw new NotImplementedException("Tile not implement yet");
                }

                _pictureBox.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right;

                _pictureBox.Image = Image.FromFile(imageFile);

                ((ISupportInitialize)_pictureBox).EndInit();
            }

        }

        public override void Start()
        {
            base.Start();
        }

        public override void Stop()
        {
            base.Stop();
        }

    }
}
